package com.baidu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.Result;
import com.baidu.mapper.MemberMapper;
import com.baidu.mapper.OrderMapper;
import com.baidu.mapper.OrderSettingMapper;
import com.baidu.pojo.Member;
import com.baidu.pojo.Order;
import com.baidu.pojo.OrderSetting;
import com.baidu.service.OrderService;
import com.baidu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderSettingMapper orderSettingMapper;

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private MemberMapper memberMapper;

	@Override
	public Result order(Map map) throws Exception {
//		检查当天是否开放预约设置功能。
		String orderDate = (String) map.get("orderDate");


		OrderSetting orderSetting = orderSettingMapper.findOrderByOrderDate(DateUtils.parseString2Date(orderDate));
		if (orderSetting == null) {
			return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);

		}


//		检查当天是否的预约人数是否已满。

//		返回的orderSetting已经封装了需要的参数
		int number = orderSetting.getNumber();
		int reservations = orderSetting.getReservations();

		if (reservations >= number) {
			return new Result(false, MessageConstant.ORDER_FULL);
		}


//		检查用户同一天是否重复预约同一个套餐

		String telephone = (String) map.get("telephone");
		Member member = memberMapper.findMemberBytelephone(telephone);

		if (member != null) {
			//判断是否重复预约
			Integer memberId = member.getId();
			String setmealId = (String) map.get("setmealId");
			Order order = new Order(memberId, orderDate, Integer.parseInt(setmealId));
			List<Order> orderList = orderMapper.findByCondition(order);

			if (orderList != null || orderList.size() > 0) {
				//表示重复预约
				return new Result(false, MessageConstant.HAS_ORDERED);

			}

		} else {
//				不是会员，需要完成会员注册
			member = new Member();
			member.setIdCard((String) map.get("idCard"));
			member.setSex((String) map.get("sex"));
			member.setPhoneNumber(telephone);
			member.setRegTime(new Date());
			member.setName((String) map.get("name"));

			memberMapper.add(member);

		}


//		上述条件都满足，预约成功,在预约表中添加一条数据。且更新当天已预约人数。

		Order order = new Order();
		order.setMemberId(member.getId());
		order.setOrderDate(orderDate);
		order.setOrderStatus(order.ORDERSTATUS_NO);
		order.setOrderType((String) map.get("orderType"));
		order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
		order.setAddress((String) map.get("address"));
		orderMapper.add(order);

		orderSetting.setReservations(orderSetting.getReservations() + 1);
		orderSettingMapper.updateOrderReservations(orderSetting);

		return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId()); //前台需要一个预约的id作为页面的跳转参数。

	}

	@Override
	public Map findById(Integer id) throws Exception {


	Map map=orderMapper.findById(id);

		Date orderDate = (Date) map.get("orderDate");

		String o = DateUtils.parseDate2String(orderDate);

		map.put("orderDate" ,o);

		return map;
	}

	@Override
	public ArrayList<Integer> findOrderByWeek(ArrayList<String> listW) {
		ArrayList<Integer> orderCount =new ArrayList<>();
		for (String s : listW) {
			int i=	orderMapper.findOrderCount(s);
			orderCount.add(i);
		}
		return orderCount;
	}
}
